{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ogr, gdal\n",
    "import os\n",
    "import pandas as pd\n",
    "path = os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = ogr.GetDriverByName(\"ESRI Shapefile\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "xzqhshp = path +\"/china/\"+ \"cn.shp\"\n",
    "outfile = path + \"/china_HULL.shp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "inDriver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "inDataSource = inDriver.Open(xzqhshp, 0)\n",
    "inLayer = inDataSource.GetLayer()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成一个包含所有要素的巨大的多边形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "geomcol = ogr.Geometry(ogr.wkbGeometryCollection)\n",
    "for feature in inLayer:\n",
    "    geomcol.AddGeometry(feature.GetGeometryRef())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算包络多边形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "convexhull = geomcol.ConvexHull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outDriver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "if os.path.exists(outfile):\n",
    "    outDriver.DeleteDataSource(outfile)\n",
    "# 创建输出\n",
    "outDataSource = outDriver.CreateDataSource(outfile)\n",
    "outLayer = outDataSource.CreateLayer(\"china_convexhull\",\n",
    "                                     geom_type=ogr.wkbPolygon)\n",
    "# 添加id字段\n",
    "idField = ogr.FieldDefn(\"id\", ogr.OFTInteger)\n",
    "outLayer.CreateField(idField)\n",
    "# 创建要素，并且将包络多边形写入到输出图层中\n",
    "featureDefn = outLayer.GetLayerDefn()\n",
    "feature = ogr.Feature(featureDefn)\n",
    "feature.SetGeometry(convexhull)\n",
    "feature.SetField(\"id\", 1)\n",
    "outLayer.CreateFeature(feature)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置为空，表示数据push到文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature = None\n",
    "inDataSource = None\n",
    "outDataSource = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
